//===============================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using System.Windows;
using P2.Cuberry.Framework.Helper;


namespace P2.Cuberry.Modules.Framework.ModuleManager
{

    public class ModulesPresenter
    {
        private IUnityContainer container;
        private IRegionManager regionManager;
        private IModulesListPresenter listPresenter;      

        public ModulesPresenter(IModulesView view, IModulesListPresenter listPresenter,        
            IUnityContainer container, IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.View = view;
            this.listPresenter = listPresenter;
            this.listPresenter.ModuleSelected += new EventHandler<DataEventArgs<BusinessEntities.Module>>(this.OnModuleSelected);
            this.listPresenter.ModuleOpened += new EventHandler<DataEventArgs<BusinessEntities.Module>>(this.OnModuleOpened);
           
            View.SetHeader(listPresenter.View);
        }

        public IModulesView View { get; set; }


        public void OnModuleSelected(object sender, DataEventArgs<BusinessEntities.Module> e)
        {
  
        }


        public void OnModuleOpened(object sender, DataEventArgs<BusinessEntities.Module> e)
        {
            try
            {
                IModulesDetailsView dv = this.container.Resolve<IModulesDetailsView>();
                
                e.Value.FillMyObject();
                Services.IModuleManagerService service = this.container.Resolve<Services.IModuleManagerService>();
                ModulesDetailsPresentationModel pm = new ModulesDetailsPresentationModel(dv, e.Value, service, container, regionManager);

                dv.FlowDirection = (P2Translator.Culture.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight);
                dv.ShowDialog();
                pm = null;
                dv = null;
                GC.Collect();
            }
            catch (System.Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }


        public void OnModuleFind(object sender, DataEventArgs<BusinessEntities.Module> e)
        {
            if (listPresenter != null)
            {
                listPresenter.FindModule(e.Value);
            }
        }


    }
}
